package com.zy.ems.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zy.ems.common.base.BaseDao;
import com.zy.ems.common.base.BaseService;
import com.zy.ems.common.page.PaginationResult;
import com.zy.ems.common.util.StringUtils;
import com.zy.ems.dao.MemberDao;
import com.zy.ems.model.MemberEntity;
import com.zy.ems.utils.util.MemberUtil;

@Service
@Transactional
public class MemberService extends BaseService<MemberEntity, Integer> {
    @Autowired
    private MemberDao memberDao;

    public MemberEntity login(String userName, String userPassword) {
        MemberEntity memberEntity = this.memberDao.getByUserName(userName);

        if (memberEntity == null) {
            return null;
        }

        userPassword = MemberUtil.getMemberPasswordWithSalt(userPassword);

        if (memberEntity.getPassword().equals(userPassword)) {
            return memberEntity;
        }

        return null;
    }

    @Override
    public BaseDao<MemberEntity, Integer> getEntityDao() {
        return this.memberDao;
    }

    /**
     * 获取学生列表
     * 
     * @param paramMap
     * @return
     */
    public PaginationResult<MemberEntity> getMemberList(
            Map<String, Object> paramMap) {

        int start = StringUtils.parseInt(paramMap.get("start"));
        int size = StringUtils.parseInt(paramMap.get("size"));

        return this.memberDao.findPageList(paramMap, start, size);
    }

    public MemberEntity getByUserName(String userName) {
        return this.memberDao.getByUserName(userName);
    }

    /**
     * 更改用户状态
     * 
     * @param userId
     * @param status
     */
    public void changeMemberStatus(int memberId, String status) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("memberId", memberId);
        param.put("status", status);

        this.memberDao.updateMemberStatus(param);
    }

    /**
     * 删除学生
     * 
     * @param memberId
     */
    public void deleteMemberById(int memberId) {
        this.changeMemberStatus(memberId, "0");
    }

    /**
     * 增加学生
     * @param memberEntity
     */
    public void addMember(MemberEntity memberEntity) {
        
    }
}
